<?php
namespace app\admin\controller;

use think\Db;
use think\Loader;

class Cate extends Base
{
    public function lst()
    {
        $cateres = Db::name('Cate')->order('id')->select();
        $this->assign('cateres', $cateres);
        return $this->fetch();
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = [
                'catename' => input('catename'),
                'keywords' => input('keywords'),
                'desc' => input('desc'),
                'type' => input('type') ? input('type') : 0,
            ];
            $validate = Loader::validate('Cate');
            if ($validate->check($data)) {
                if (Db::name('cate')->insert($data)) {
//                    $this->redirect('index/index/index');
                    return $this->success('添加栏目成功', 'lst');
                } else {
                    return $this->success('添加栏目失败');
                }
            } else {
                return $this->error($validate->getError());
            }
            return;
        }
        return $this->fetch();
    }

    public function del()
    {
        if (db('cate')->delete(input('id'))) {
            return $this->success('删除栏目成功！', 'lst');
        } else {
            return $this->success('删除栏目失败！');
        }
    }

    public function edit()
    {
        if (request()->isPost()) {
            $data = [
                'id' => input('id'),
                'catename' => input('catename'),
                'keywords' => input('keywords'),
                'desc' => input('desc'),
                'type' => input('type') ? input('type') : 0,
            ];
            $validate = Loader::validate('Cate');
            if ($validate->check($data)) {
                if (Db::name('cate')->update($data)) {
                    return $this->success('修改栏目成功', 'lst');
                } else {
                    return $this->success('修改栏目失败');
                }
            }else {
                return $this->error($validate->getError());
            }
            return;
        }
        $id = input('id');
        $cates = db('cate')->where('id', $id)->find();
        $this->assign('cates', $cates);
        return $this->fetch();
    }
}